#!/bin/bash

#// *************************************************************************
#// * COPYRIGHT 2004, EMULEX CORPORATION                                    *
#// * 3333 Susan Street, Costa Mesa, CA 92626                               *
#// *                                                                       *
#// * All rights reserved.  This computer program and related documentation *
#// * is protected by copyright  and distributed under licenses restricting *
#// * its use,  copying,  distribution  and decompilation.    This computer *
#// * program  and its  documentation  are CONFIDENTIAL  and a TRADE SECRET *
#// * of EMULEX CORPORATION.   The receipt or  possession of  this  program *
#// * or its documentation does not  convey rights to reproduce or disclose *
#// * its  contents,  or to  manufacture, use, or sell anything that it may *
#// * describe, in whole or in part,  without the specific  written consent *
#// * of  EMULEX CORPORATION.   Any reproduction  of  this program  without *
#// * the express  written  consent  of EMULEX  CORPORATION  is a violation *
#// * of the  copyright laws  and may  subject you to  criminal prosecution.*
#// *************************************************************************

#
# This script uninstalls HBAnyware or HBAnywareSSC for Linux
#
                                                                                                                        

opt="$1"

SSCUninstall=0
LOGFILE="/usr/src/lpfcdfc/utils-install.log"

# Function:  log_message()
#
# Description:
#   Print the strings passed as parameters to both STDOUT as well as
#   to a log file.
#
# Parameters:
#   A series of strings.
#
# Returns:
#   0 on success.
log_message()
{
    local ECHO_OPTIONS
    LOGDIRECTORY=${LOGFILE%/*}
    if [ ! -d ${LOGDIRECTORY} ] ; then
	mkdir -p ${LOGDIRECTORY}
    fi

    if [ $# -eq 0 ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ -z "$1" ] ; then
	echo "" | tee -a ${LOGFILE}
    elif [ "$1" = "-init" ] ; then
	echo "$0 - $(date)" >> ${LOGFILE}
    else
	while [ -n "$1" ] ; do
	    if [ "${1:0:1}" = "-" ] ; then
		ECHO_OPTIONS="${1}"
	    else
		echo ${ECHO_OPTIONS} "$1" | tee -a ${LOGFILE}
	    fi
	    shift
	done
    fi

    return 0
}

# Initialize the log file.
log_message -init

# Check if HBAnywareSSC uninstall option is passed in
if [ -n "$opt" ];then
    opt=`echo $opt | tr A-Z a-z`
    if [ "$opt" = "ssc" ];then
        SSCUninstall=1
    fi
fi

# Get environment data
OS=`uname -s`
Arch=`uname -m`
if [ "$Arch" = "i686" ];then
    Arch="i386"
fi
if [ "$Arch" = "x86_64" ];then
    Arch="i386"
    X86_64=1
else
    X86_64=0
fi
GCC_Ver=`gcc -v 2>&1 | grep version | awk '{ print $3 }' | awk -F. '{ print $1 }'`
OS_Type=`if [ -d /usr/src/redhat ];then echo "RH";else echo "SL";fi`
if [ "$OS_Type" = "RH" ];then
    if [ "$GCC_Ver" -eq 2 ];then
        OS_Ver="rhel-2.1"
    else
        OS_Ver="rhel-3.0"
    fi
else
    OS_Ver="sles-8.0"
fi

                                                                                                                          
# This is a Linux install only, if we're not on Linux then stop
if [ "$OS" != "Linux" ];then
    log_message "This installation is for Linux only! Exiting..."
    exit
fi


# HBAnyware Uninstall
if [ $SSCUninstall -eq 0 ];then

    # Uninstall any previousely installed HBAnywareSSC
    Cur_HBA=`rpm -qa | grep HBAnywareSSC`
    if [ -n "$Cur_HBA" ];then
        stay_in_loop=1
        while [ $stay_in_loop -eq 1 ]
        do 
           echo
           echo "HBAnyware SSC package currently installed on this machine."
           echo "Must first uninstall HBAnyware SSC package before uninstalling HBAnyware."
           echo "Do you want to uninstall HBAnyware SSC package now?" 
           echo
           echo
           echo -n "Enter the letter 'y' if yes, or 'n' if no "

           # Obtain user selection for "uninstall SSC?" reply
           read ssc_uninstall
           if [ "$ssc_uninstall" = "y" ];then
               log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
               rpm -e "$Cur_HBA"
               stay_in_loop=0;
           fi
           if [ "$ssc_uninstall" = "Y" ];then
               log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
               rpm -e "$Cur_HBA"
               stay_in_loop=0;
           fi
           if [ "$ssc_uninstall" = "n" ];then
               log_message "HBAnyware not uninstalled."
               stay_in_loop=0;
			   exit
           fi
           if [ "$ssc_uninstall" = "N" ];then
               log_message "HBAnyware not uninstalled."
               stay_in_loop=0;
			   exit
           fi
        done
        echo ""
    fi

    Cur_HBA=`rpm -qa | grep HBAnyware | grep -v SSC`
    if [ -n "$Cur_HBA" ];then
        log_message "Uninstalling HBAnyware -- $Cur_HBA -- ..."
        rpm -e --nodeps "$Cur_HBA"
        log_message ""
    fi
 

    # Uninstall dfc libs and utility
    rm -f /usr/sbin/lpfc/dfc
    rm -f /usr/lib/libdfc.so
    rm -f /usr/lib/libdfc.a
    rm -f /usr/lib64/libdfc.so
    rm -f /usr/lib64/libdfc.a
    rm -f /usr/sbin/lpfc/lun_scan


    # Uninstall HBAAPI libs
    rm -f /usr/lib/libHBAAPI.so
    rm -f /usr/lib/libemulexhbaapi.so
    rm -f /usr/lib64/libHBAAPI.so
    rm -f /usr/lib64/libemulexhbaapi.so


    # Uninstall lputil utility
    rm -f /usr/sbin/lpfc/lputil

    # remove semaphore lock file directory
    rm -rf /usr/sbin/hbanyware/misc

    # Uninstall JRE
    rm -rf /usr/sbin/hbanyware/jre

    rm -f /usr/sbin/hbanyware/READ*.txt
    rmdir --ignore-fail-on-non-empty /usr/sbin/hbanyware
    rmdir --ignore-fail-on-non-empty /usr/sbin/lpfc
    rm -f /usr/lib/lib_unix-rm/lib_rm_unix.so
    rm -f /usr/lib64/lib_unix-rm/lib_rm_unix.so

    log_message "HBAnyware Uninstallation complete."
    log_message ""

    # Uninstall dfc helper module
    cd enterprise_kitfiles
    ./lpfcdfc-install -u
    EXITCODE=$?
    if [ ${EXITCODE} -eq 10 ];then
	log_message ""
	log_message "Success with WARNINGS uninstalling Application Helper"
	log_message "Module.  Please refer to the following file for more"
	log_message "information about these warnings:"
	log_message "   ${LOGFILE}"
	log_message ""
    elif [ ${EXITCODE} -ne 0 ];then
        log_message "ERROR: Application Helper Module uninstallation failed."
    fi
	cd ..

#  HBAnywareSSC Uninstallation
else   
    # Uninstall any previousely installed HBAnywareSSC
    Cur_HBA=`rpm -qa | grep HBAnywareSSC`
    if [ -n "$Cur_HBA" ];then
        log_message "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
        rpm -e "$Cur_HBA"
    fi

    log_message "HBAnywareSSC Uninstallation complete."
    log_message ""
fi


